﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Configuration;
using FCNS.KeepMoney.Util;

namespace FCNS.KeepMoney.Forms
{
    public partial class CalcForm : ParentForm
    {
        enum Oper
        {
            Add,
            Subtract,
            Equal,
            Multiply,
            Divide
        }

        decimal pre = 0;//前一个计算结果
        string alreq = null;//利用前一个计算结果的标记
        string prestr;//前一个数值字符串
        string flag = null;//前一个运算符标记
        string iseq = "eq";//等号标记
        string lastchar;//当前运算符标记
        string mude = null;//保存前一个*或/号
        string premude;//保存前一个被除数或乘数
        decimal result;//保存按等号后的结果

        public CalcForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
            this.Icon = Properties.Resources.rmb;
        }

        private void CalcForm_Load(object sender, EventArgs e)
        {
            textBoxString.Text = "0.";

            if (this.Owner is MainForm)
                this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2, Owner.Top + 5);
            else
                this.Location = MousePosition;
        }

        private void add_Click(object sender, EventArgs e)
        {
            operate(Oper.Add);
        }

        private void sub_Click(object sender, EventArgs e)
        {
            operate(Oper.Subtract);
        }

        private void equ_Click(object sender, EventArgs e)
        {
            operate(Oper.Equal);
            if (Owner is EntryForm)
                ((EntryForm)Owner).Value = result;

        }

        private void mul_Click(object sender, EventArgs e)
        {
            operate(Oper.Multiply);
        }

        private void div_Click(object sender, EventArgs e)
        {
            operate(Oper.Divide);
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            prestr = null;
            lastchar = null;
            textBoxString.Text = "0.";
            pre = 0;
            flag = null;
            iseq = "eq";
        }


        private void num0_Click(object sender, EventArgs e)
        {
            caculate("0");
        }

        private void num1_Click(object sender, EventArgs e)
        {
            caculate("1");
        }

        private void num2_Click(object sender, EventArgs e)
        {
            caculate("2");
        }

        private void num3_Click(object sender, EventArgs e)
        {
            caculate("3");
        }

        private void num4_Click(object sender, EventArgs e)
        {
            caculate("4");
        }

        private void num5_Click(object sender, EventArgs e)
        {
            caculate("5");
        }

        private void num6_Click(object sender, EventArgs e)
        {
            caculate("6");
        }

        private void num7_Click(object sender, EventArgs e)
        {
            caculate("7");
        }

        private void num8_Click(object sender, EventArgs e)
        {
            caculate("8");
        }

        private void num9_Click(object sender, EventArgs e)
        {
            caculate("9");
        }

        private void dot_Click(object sender, EventArgs e)
        {
            if (prestr == null)
                return;

            if (lastchar == ".")
                return;

            if (prestr.Contains("."))
                return;

            caculate(".");
            lastchar = ".";
        }


        private void buttonExchange_Click(object sender, EventArgs e)
        {
            if (this.Width > 300)
                this.Width -= 210;
            else
            {

                listViewExchange.Items.Clear();
                buttonExchange.Text = AppDefines.exchangeDay + " " + LanguageUtil.更新;
                foreach (ExchangeField field in ExchangeField.GetExchangeField)
                {
                    ListViewItem item = new ListViewItem(field.Flag);
                    item.SubItems.Add(field.Name);
                    item.SubItems.Add(field.Rate);

                    listViewExchange.Items.Add(item);
                }
                this.Width += 210;
            }
        }

        private void OnExchangeMoney(object sender, EventArgs e)
        {
            bool b = (listViewExchange.SelectedItems.Count == 0);
            buttonExchangeOut.Enabled = !b;
            buttonExchangeIn.Enabled = !b;
            if (b)
                return;

            listViewExchange.Tag = decimal.Parse(listViewExchange.SelectedItems[0].SubItems[2].Text);
        }

        private void buttonExchangeOut_Click(object sender, EventArgs e)
        {
            result = decimal.Round(result * 100 / (decimal)listViewExchange.Tag,Program.AppConfig.Money.DecimalPlaces);
            textBoxString.Text = result.ToString();
            if (Owner is EntryForm)
                ((EntryForm)Owner).Value = result;
        }

        private void buttonIn_Click(object sender, EventArgs e)
        {
            result =decimal.Round(result * (decimal)listViewExchange.Tag /100,Program.AppConfig.Money.DecimalPlaces) ;
            textBoxString.Text = result.ToString();
            if (Owner is EntryForm)
                ((EntryForm)Owner).Value = result;
        }
    }
}